/** 
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * 
 *SdProjectResourcePoolController
 * SD部门审批人资源池
 * 
 * 修改纪录
 * 2021-12-07 版本：1.0 jiangtao 创建。
 * @version 版本：1.0
 * @author 作者：jiangtao
 * 创建日期：2021-12-07
 */

package com.topscomm.gpm.controller;

import java.util.List;
import java.util.Map;

import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.gpm.controller.auto.GpmProjectResourcePoolControllerAuto;
import com.topscomm.pub.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: jiangtao
 * @date: 2021-12-07
 * @description:SD部门审批人资源池Controller
 * @modify:
 */
@RestController
@RequestMapping("sd/projectResourcePool")
public class GpmProjectResourcePoolController extends GpmProjectResourcePoolControllerAuto implements IGpmProjectResourcePoolController {
	@Autowired
	private CboDeptService cboDeptService;
	/**
	 * @author: jiangtao
	 * @date: 2021-12-07
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (usercode like '%{0}%' or username like '%{0}%')", value);
		}

		if(paraMap.containsKey("qry_dept")){
			String value = paraMap.get("qry_dept").toString();
			if(!StringUtil.isEmpty(value)){
				sql.appendSingle(" and deptname like '%{0}%'",value);
			}
		}
		if(paraMap.containsKey("qry_projectlevel")){
			String value = paraMap.get("qry_projectlevel").toString();
			if(!StringUtil.isEmpty(value)){
				String[] split = value.split(",");
				if(split.length>1) {
					for (int i = 0; i < split.length; i++) {
						if (i == 0) {
							sql.appendSingle("and (projectlevel like '%{0}%'", split[i]);
						} else if (i == split.length - 1) {
							sql.appendSingle(" or projectlevel like '%{0}%')", split[i]);
						} else {
							sql.appendSingle(" or projectlevel like '%{0}%'", split[i]);
						}
					}
				}else{
					sql.appendSingle("and projectlevel like '%{0}%'",split[0]);
				}
			}
		}
		if(paraMap.containsKey("qry_projecttype")){
			String value = paraMap.get("qry_projecttype").toString();
			if(!StringUtil.isEmpty(value)){
				sql.appendSingle(" and  projecttype in ({0})",value);
			}
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: jiangtao
	 * @date: 2021-12-07
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	@Override protected void beforeInsert(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeInsert(dataMap, paraMap);
		List<Map<String, Object>> userList = (List<Map<String, Object>>) paraMap.get("userList");
		if(userList.size()>0) {
			Map<String,Object> user = userList.get(0);
			CboDeptEntity cboDeptEntity = cboDeptService
					.queryById(ConvertUtil.convertToString(user.get("deptid")));
			paraMap.put("deptcode",cboDeptEntity.getCode());
			dataMap.put("deptcode",cboDeptEntity.getCode());
		}
	}
}

